// pages/index/list.js
const nt_util = require('../../utils/nt_util.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    latitude:'',
    longitude:'',
    hasMore: true,
    loadding: false,
    limit: 10,
    offset: 0,
    typeId:'',
    typename:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    this.setData({ 
      typeId: options.typeId,
      typename: options.typename,
      })
    wx.setNavigationBarTitle({
      title: this.data.typename
    }),
    //1、获取当前位置坐标
    wx.getLocation({
      type: 'gcj02',// 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
      success: function (res) {
        // nt_util.log(res);
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })
        nt_util.saveUserLocation(res.latitude, res.longitude)
      },
      
      complete: function (e) {
        that.getData();
      }
    })
  },

  getData: function (e) {
    nt_util.that = this;
    wx.showLoading({
      title: '加载中...',
    })
    // 获取列表数据
    nt_util.httpRequest('api/life/v1/getStoreInfoList', 'POST', {
      typeId: nt_util.that.data.typeId,
      lng: nt_util.that.data.longitude,
      lat: nt_util.that.data.latitude,
      limit: nt_util.that.data.limit,
      offset: nt_util.that.data.offset
    }, function (data) {
      nt_util.that.setData({
        items: data.data.list,
        loadding: false,
        offset: nt_util.that.data.offset + nt_util.that.data.limit
      });
      //判断是否还有数据
      if (data.data.list.length < nt_util.that.data.limit) {
        nt_util.that.setData({
          hasMore: false
        });
      }
      // nt_util.nt_log(nt_util.that.data.data);
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  //快速拨打电话号码
  quickCall: function (event) {
    var phone = event.currentTarget.dataset.phone;
    //判断电话号码是否可用
    if (phone != null) {
      wx.makePhoneCall({
        phoneNumber: phone
      })
    }

  },

  loadMore: function (event) {
    if ((!this.data.loadding) && this.data.hasMore) {

      //防止多次触底 触发多次
      this.setData({
        loadding: true
      });

      nt_util.httpRequest('api/life/v1/getStoreInfoList', 'POST', {
        typeId: nt_util.that.data.typeId,
        lng: nt_util.that.data.longitude,
        lat: nt_util.that.data.latitude,
        limit: nt_util.that.data.limit,
        offset: nt_util.that.data.offset
      }, function (data) {
        var _items = nt_util.that.data.items;
        _items = _items.concat(data.data.list);
        nt_util.that.setData({
          items: _items,
          loadding: false,
          offset: nt_util.that.data.offset + nt_util.that.data.limit
        });

        //判断是否还有数据
        if (data.data.list.length < nt_util.that.data.limit) {
          nt_util.that.setData({
            hasMore: false
          });
        }
      });
    }
  },
  scroll: function (event) {

  },

  //点击跳转到详细页面
  tapItem: function (event) {

    var dataset = event.currentTarget.dataset;
    //拿到点击的index下标
    var index = dataset.index
    // var queryObj = nt_util.deleteEmptyProperty(nt_util.that.data.items[index]);
    var _queryObj = nt_util.that.data.items[index];
    var queryObj = {
      store_pic: _queryObj.store_pic,
      name: _queryObj.name,
      store_name: _queryObj.store_name,
      phone: _queryObj.phone,
      phone_bak: _queryObj.phone_bak,
      address: _queryObj.address,
      lat: _queryObj.lat,
      lng: _queryObj.lng
    }
    //将对象转为string
    var queryBean = JSON.stringify(queryObj)
    var url = '/pages/index/newd?queryBean=' + queryBean;
    wx.navigateTo({
      url: url
    })
  }

})